<?php

namespace app\admin\controller;

class AlarmRecord extends Backend
{
    protected $model = null;

    protected $with = ['seal', 'user', 'appForm'];

    protected $needSelectCompany = true;
    protected $beforeActionList = [
        'getCurrentCompany',
        'validateCompany' => ['only' => 'show,delete'],
        "seals"           => ['only' => 'index'],
    ];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\AlarmRecord();
    }


    public function index()
    {
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $where = [];
        $params = $this->request->param();
        if (isset($params['sealId']) && $params['sealId']) {
            $where['seal_id'] = $params['sealId'];
        }

        if ($params['startTime'] && $params['endTime']) {
            $where['create_time'] = ['between', [strtotime($params['startTime']), strtotime($params['endTime'] . ' 23:59:59')]];
        }
        $where['company_id'] = $this->company->id;
        $list = $this->model
            ->with($this->with ? $this->with : null)
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        $this->assign('list', $list);
        $this->assign('params', $params);
		$condition['is_del']=array('eq',0);
        $condition['company_id']=$this->company->id;
        $seals =db('seal')->where($condition)->select();
        $this->assign('seals', $seals);
        return $this->fetch();
    }
    public function dc(){
        $company = $this->company;
        $company_id=$company->id;
        $where['company_id'] = $company_id;

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $list = $this->model
            ->with($this->with ? $this->with : null)
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        vendor('phpexcel/PHPExcel');
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //exit($objPHPExcel);
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '印章名称')
                ->setCellValue('B1', '异常描述')
                ->setCellValue('C1', '报警时间')
                ->setCellValue('D1', '使用人');

        //设置A列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
                    ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置单元格宽度
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);



          
        for($i=0;$i<count($list);$i++){
            $objPHPExcel->getActiveSheet()->setCellValue('A'.($i+2),$list[$i]['seal']['name']);//ID
            $objPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$list[$i]['reason_txt']);//标签码
            $objPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$list[$i]['create_time']);//防伪码
            $objPHPExcel->getActiveSheet()->setCellValue('D'.($i+2),$list[$i]['user']['username']);//ID
        }
        //7.设置保存的Excel表格名称
        $filename = 'yz'.date('ymd',time()).'.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('cs');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download"); 
        header("Content-Type: application/octet-stream"); 
        header("Content-Type: application/download"); 
        header('Content-Disposition:inline;filename="'.$filename.'"'); 
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        exit;



    }

    protected function validateCompany()
    {
        if (is_null($this->company)) {
            $this->error('请选择企业');
        }
        $entity = $this->model->find(input('id'));
        if ($entity->company_id != $this->company->id) {
            $this->error('非法操作');
        }
    }

    protected function seals()
    {
        $sealModel = new \app\admin\model\Seal();
        $seals = $sealModel->where('company_id', $this->company->id)->select();
        $this->assign('seals', $seals);
    }


}
